clear all
set more off

program main
    plot_shares, category("NewsMajor") title("Major News Sites")           max_fb(300) max_tw(30)   max_r(15)
    plot_shares, category("NewsSmall") title("Small News Sites")           max_fb(6)   max_tw(0.21) max_r(36)
    plot_shares, category("Other")     title("Business and Culture Sites") max_fb(120) max_tw(9)    max_r(18)
    plot_shares, category("Fake")      title("Fake News Sites")            max_fb(180) max_tw(6)    max_r(60)

    * Combine graphs
    graph combine g_facebook_NewsMajor g_facebook_NewsSmall g_facebook_Other g_facebook_Fake, ///
      graphregion(color(white)) rows(2)
    graph export "TrendFacebookSite.eps", replace
    graph combine g_twitter_NewsMajor g_twitter_NewsSmall g_twitter_Other g_twitter_Fake, ///
      graphregion(color(white)) rows(2)
    graph export "TrendTwitterSite.eps", replace
    graph combine g_ratio_NewsMajor g_ratio_NewsSmall g_ratio_Other g_ratio_Fake, ///
      graphregion(color(white)) rows(2)
    graph export "TrendRatioFacebookTwitterSite.eps", replace
end
   
program plot_shares
    syntax, category(string) title(string) max_fb(string) max_tw(string) max_r(string)

    use "Site`category'.dta", clear
    keep if yrmo >= m(2015m1) & yrmo <= m(2018m7)
    encode site, gen(site_num)
    sum site_num
    local N = r(max)
    collapse (sum) SharesFacebook SharesTwitter, by(quarter yrmo)
    collapse (mean) SharesFacebook SharesTwitter, by(quarter)
    replace SharesFacebook = SharesFacebook / 1000000
    replace SharesTwitter  = SharesTwitter  / 1000000
    gen ratio = SharesFacebook / SharesTwitter

    plot_trend, var(SharesFacebook) name(facebook_`category') max(`max_fb') obs(`N') ///
      title("`title'") ytitle("Facebook engagements (million)")
    plot_trend, var(SharesTwitter)  name(twitter_`category')  max(`max_tw') obs(`N') ///
      title("`title'") ytitle("Twitter shares (million)")
    plot_trend, var(ratio)          name(ratio_`category')    max(`max_r')  obs(`N') ///
      title("`title'") ytitle("Facebook engagements / Twitter shares")
end

program plot_trend
    syntax, var(string) name(string) max(string) obs(string) title(string) ytitle(string)

    local interval = `max' / 3
    if strpos("`name'", "Fake") {
        local option "lcolor(maroon)"
    }
    twoway line `var' quarter, graphregion(color(white)) name(g_`name', replace) ///
      ytitle("`ytitle'", size(3)) ylabel(0(`interval')`max', angle(0)) ///
      xtitle("") tlabel(2015q1(4)2018q1, angle(0) format(%tqCY) tlength(*2)) tmtick(##4) ///
      title("`title'", size(4)) note("Number of sites: `obs'", size(4)) `option'
end

main
